// pages/route/detail/detail.js
const http = require("../../../utils/http.js")
const utils = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    routeId: '',

    route: {},

    version: '1.0.0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    http.get("/get")
      .then(res => {
        that.setData({
          version: res.data
        })
      })
    
    that.setData({
      routeId: options.id,
    })
    
   
    
  },
  initData() {
    let that = this
    let routeId = that.data.routeId
    if (routeId == null) {
      return;
    }
    http.get("/route/getDetail", {
      routeId: routeId
    }).then(res => {
      let routeData = res.data.data;

      let date = utils.dateFormat(routeData.route.startDayDate)
      routeData.route.startDayDate = date

      for (let i = 0; i < routeData.routeDay.length; i++) {
        let date = utils.dateFormat(routeData.routeDay[i].routeDay.dayDate)
        routeData.routeDay[i].routeDay.dayDate = date
      }
      that.setData({
        route: routeData
      })
    })
  },

  settings(e) {
    let routeid = e.currentTarget.dataset.routeid
    wx.navigateTo({
      url: "/pages/route/setting/setting?routeId=" + routeid
    })
  },
  longtap(e) {
    let that = this
    let route = that.data.route
    if (route.route.releaseStatus == 1) {
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定删除这个景点？',
      success(res) {
        if (res.confirm) {
          http.get("/routeDayPoi/delete", {
            id: e.currentTarget.dataset.id
          }).then(res => {
            if (res.data.status == 200) {
              that.initData();
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  toPoiDetail(e) {
    let poiId = e.currentTarget.dataset.poiid
    wx.navigateTo({
      url: '/pages/location/poidetail/poidetail?poiId=' + poiId,
    })
  },

  addPoi(e) {
    let route = this.data.route
    if (route.route.releaseStatus == 1){
      wx.showToast({
        title: '该行程已发布无法添加景点',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.navigateTo({
      url: "/pages/route/addpoi/addpoi?routeId=" + e.currentTarget.dataset.routeid
    })
  },

  release(e) {
    let data = this.data.route;
    for (let i = 0; i < data.routeDay.length; i++) {
      if (data.routeDay[i].routeDayPoi.length == 0) {
        wx.showToast({
          title: '还有未添加景点的DAY哟',
          icon: 'none',
          duration: 1500
        })
        return;
      }
    }

    wx.navigateTo({
      url: '/pages/route/release/release?routeId=' + e.currentTarget.dataset.routeid
    })
  },

  preview(e){
    wx.navigateTo({
      url: '/pages/recommend/components/choiceness/route/route?routeId=' + e.currentTarget.dataset.routeid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})